Grokking Microsoft Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Microsoft Easy Problems
Buddy Strings (easy)
Solution: Buddy Strings
Can Place Flowers (easy)
Solution: Can Place Flowers
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Maximum Number of Balloons (easy)
Solution: Maximum Number of Balloons
Binary Tree Path Sum (easy)
Solution: Binary Tree Path Sum
Pair with Target Sum (easy)
Solution: Pair with Target Sum
Buy Two Chocolates (easy)
Solution: Buy Two Chocolates
Root Equals Sum of Children (easy)
Solution: Root Equals Sum of Children
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Min Cost Climbing Stairs (easy)
Solution: Min Cost Climbing Stairs
Plus One (easy)
Solution: Plus One
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Find the Index of the First Occurrence in a String (easy)
Solution: Find the Index of the First Occurrence in a String
Merge Two Sorted Lists (easy)
Solution: Merge Two Sorted Lists
Microsoft Medium Problems
Nth Digit (medium)
Solution: Nth Digit
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Find Original Array From Doubled Array (medium)
Solution: Find Original Array From Doubled Array
Reverse Words in a String II (medium)
Solution: Reverse Words in a String II
Longest Arithmetic Subsequence (medium)
Solution: Longest Arithmetic Subsequence
Matchsticks to Square (medium)
Solution: Matchsticks to Square
Score of Parentheses (medium)
Solution: Score of Parentheses
Maximum Length of a Concatenated String with Unique Characters (medium)
Solution: Maximum Length of a Concatenated String with Unique Characters
Inorder Successor in BST (medium)
Solution: Inorder Successor in BST
Distribute Coins in Binary Tree (medium)
Solution: Distribute Coins in Binary Tree
Spiral Matrix (medium)
Solution: Spiral Matrix
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Rotate Image (medium)
Solution: Rotate Image
Combinations (medium)
Solution: Combinations
Maximum Level Sum of a Binary Tree (medium)
Solution: Maximum Level Sum of a Binary Tree
Multiply Strings (medium)
Solution: Multiply Strings
Best Time to Buy and Sell Stock II (medium)
Solution: Best Time to Buy and Sell Stock II
Largest Palindromic Number (medium)
Solution: Largest Palindromic Number
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Next Permutation (medium)
Solution: Next Permutation
132 Pattern (medium)
Solution: 132 Pattern
Valid Sudoku (medium)
Solution: Valid Sudoku
Number of Islands (medium)
Solution: Number of Islands
Buildings With an Ocean View (medium)
Solution: Buildings With an Ocean View
Regular Expression Matching (hard)
Solution: Regular Expression Matching
First Missing Positive (hard)
Solution: First Missing Positive
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Sliding Window Maximum (hard)
Solution: Sliding Window Maximum
Count Unique Characters of All Substrings of a Given String (hard)
Solution: Count Unique Characters of All Substrings of a Given String
Word Ladder (hard)
Solution: Word Ladder
Word Ladder II (hard)
Solution: Word Ladder II
Number of Flowers in Full Bloom (hard)
Solution: Number of Flowers in Full Bloom
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Solution: Inorder Successor in BST

Problem Statement

Given a root node of the binary search tree and node p, return the value of the in-order successor of node p in the given tree. If the given node has no in-order successor, return -1.

The in-order successor of a node is the next node in the in-order traversal of the BST, which means it is the node with the smallest value greater than the given node.

Examples

Example 1:

  • Input: root = [3,2,4,1], p = 2
Image
  • Expected Output: 3
  • Justification: In the in-order traversal [1,2,3,4], the next node after 2 is 3, making it the in-order successor.

Example 2:

  • Input: root = [8,3,10,1,6,null,14,null,null,4,7,13], p = 6
Image
  • Expected Output: 10
  • Justification: The in-order traversal of the tree is [1,3,4,6,7,8,10,13,14]. The smallest value greater than 6 is 7, making it the in-order successor.

Example 3:

  • Input: root = [15,10,20,8,12,16,25], p = 25
Image
  • Expected Output: -1
  • Justification: In the in-order traversal [8,10,12,15,16,20,25], there is no node after 25. Hence, the expected output is -1.

Solution

To address the problem of finding the in-order successor in a Binary Search Tree (BST), our strategy capitalizes on the BST's ordered nature. The in-order successor of a node is the node that appears immediately after the given node when the tree is traversed in an in-order manner (left, root, right).

For any node p, if it has a right subtree, its in-order successor is the left-most node in its right subtree. Conversely, if the node has no right subtree, the successor is one of its ancestors, specifically the one that p would be the left child of in the path from root to p. This insight leads to a two-fold approach: first, descend towards the given node p, tracking the last node encountered that is greater than p as a potential successor; second, if p has a right child, override the tracked ancestor with the left-most child of p's right subtree. This method is efficient because it traverses each relevant part of the tree at most once, directly homing in on the successor by leveraging the BST property that left children are less than their parents and right children are greater.

Step-by-Step Algorithm

  • Start from the root of the BST.
  • Initialize a variable to store the potential successor as -1 or an equivalent value indicating no successor found.
  • While the current node is not null:
    • If the value of the current node is less than or equal to the p, move to the right child.
    • If the value of the current node is greater than the p, update the potential successor to the current node and move to the left child.
  • Continue this process until the entire tree is traversed or the exact match is found.
  • Return the value of the potential successor if found; otherwise, return -1 indicating no successor.

Algorithm Walkthrough

Let's consider the input: root = [8,3,10,1,6,null,14,null,null,4,7,13], p = 6

Let's walk through the algorithm to find the in-order successor of p = 6 using the same tree structure as before:

         8
       /   \
      3     10
     / \      \
    1   6      14
       / \    /
      4   7  13
  1. Start at the root of the BST, which is 8. Since 6 is less than 8, we move to the left child of 8, which is 3, to find 6. successor = 8

  2. Move to the right child of 3, as 3 is not greater than 6.

  3. Move to the right child of 6, as 6 is not greater than 6.

  4. Inspect the right subtree of 6, which leads us to 7. Since 7 does not have a left child, it is the left-most node in the right subtree of 6 and thus the in-order successor of 6.

  5. End of Algorithm: The in-order successor of the node with value 6 in the given BST is 7.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

  • Time Complexity: The algorithm has a time complexity of , where h is the height of the tree. This is because we are traversing the tree from root to leaf, and in the worst case, we might have to traverse the height of the tree.
  • Space Complexity: The space complexity is since we are only using a constant amount of extra space for the variables, regardless of the size of the input tree.
Inorder Successor in BST (medium)
Distribute Coins in Binary Tree (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis